package com.atguigu.tingshu.user.strategy.fac;

import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.user.strategy.impl.AlbumPaidRecordStrategy;
import com.atguigu.tingshu.user.strategy.impl.TrackPaidRecordStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2024-08-23 10:35
 */
@Slf4j
@Component
public class StrategyFactory {

    /**
     * 通过Spring注入机制将PaidRecordStrategy接口下所有实现类对象自动注入到Map
     * Map中Key 为策略类型-策略实现类Bean名称(1001,1002,1003)
     * Map中Value 具体策略实现类对象
     */
    @Autowired
    private Map<String, PaidRecordStrategy> strategyMap;


    /**
     * 根据策略类型获取对应策略实现类对象
     *
     * @param strategy
     * @return
     */
    public PaidRecordStrategy getStrategy(String strategy) {
        if (strategyMap.containsKey(strategy)) {
            return strategyMap.get(strategy);
        }
        throw new RuntimeException("策略类型不存在");
    }
}
